<template>
  <div class="top-info flex-center">
    <div class="numbers">
      <span>个人问卷（{{ surveys.length }}）</span>
    </div>
    <div class="radio-sort">
      <div @click="type = 'allSurvey'">
        <input type="radio" name="survey-type" id="m-all" checked />
        <label for="m-all">所有问卷</label>
      </div>
      <div @click="type = 'stopSurvey'">
        <input type="radio" name="survey-type" id="m-stop" />
        <label for="m-stop">已结束问卷</label>
      </div>
      <div @click="type = 'collectSurvey'">
        <input type="radio" name="survey-type" id="m-collect" />
        <label for="m-collect">待收集问卷</label>
      </div>
    </div>
  </div>
  <div class="survey-area">
    <div class="wrap">
      <div class="survey-item" v-for="survey in surveys" @mouseleave="$event.currentTarget.classList.remove('hover')" @mouseenter="$event.currentTarget.classList.add('hover')" :key="survey.id">
        <span class="tag" :class="{ over: survey.over }">{{ survey.over ? '已停止' : '收集中' }}</span>
        <ul class="survey-item__config">
          <li>
            <span>{{ survey.collect_num }} 份</span>
          </li>
          <li>
            <span>{{ survey.time_format }}</span>
          </li>
          <li class="li-svg" @click="toggleOver(survey.id)">
            <span class="svg">
              <svg v-show="survey.over" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" class="coral-icon coral-icon-start_line icon stop"><path d="M10 15.694c0 .542.712.814 1.128.43l3.679-3.39a.582.582 0 00.193-.43.582.582 0 00-.193-.431l-3.68-3.39C10.713 8.1 10 8.37 10 8.913v6.78zm1.139-11.65a7.995 7.995 0 018.816 8.817c-.382 3.693-3.4 6.712-7.094 7.095a7.995 7.995 0 01-8.816-8.817c.382-3.693 3.4-6.712 7.094-7.095M2.14 13.697c.686 4.138 4.024 7.476 8.162 8.162 6.83 1.132 12.688-4.727 11.556-11.556-.686-4.138-4.023-7.476-8.162-8.162C6.867 1.009 1.009 6.868 2.14 13.697z" fill-rule="evenodd"></path></svg>
              <svg v-show="!survey.over" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" class="coral-icon coral-icon-suspend_line icon stop"><path d="M10 8a1 1 0 011 1v6a1 1 0 01-2 0V9a1 1 0 011-1zm4 0a1 1 0 011 1v6a1 1 0 01-2 0V9a1 1 0 011-1zm-2.861-3.956a7.995 7.995 0 018.816 8.817c-.382 3.693-3.4 6.712-7.094 7.095a7.995 7.995 0 01-8.816-8.817c.382-3.693 3.4-6.712 7.094-7.095M2.14 13.697c.686 4.138 4.024 7.476 8.162 8.162 6.83 1.132 12.688-4.727 11.556-11.556-.686-4.138-4.023-7.476-8.162-8.162C6.867 1.009 1.009 6.868 2.14 13.697z" fill-rule="evenodd"></path></svg>
            </span>
            <span>{{ survey.over ? '收集' : '停止' }}</span>
          </li>
          <li class="li-svg" :data-id="survey.id" @click="toStatistics">
            <span class="svg">
              <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" class="coral-icon coral-icon-record_chart_line icon stop"><path d="M11 2.047V4.06c-3.937.476-7 3.833-7 7.896 0 4.388 3.57 7.958 7.959 7.958 4.034 0 7.369-3.021 7.882-6.917h2.02c-.52 5.01-4.754 8.917-9.902 8.917-5.5 0-9.96-4.458-9.96-9.958 0-5.177 3.95-9.428 9-9.91zM12 2c5.48.022 9.917 4.47 9.917 9.957l-.002.04H12zm2 2.266v5.732h5.673A8.001 8.001 0 0014 4.266z" fill-rule="evenodd"></path></svg>
            </span>
            <span>统计</span>
          </li>
          <li>
            <button :data-id="survey.id" class="copy" @click="copyUrl">复制链接</button>
          </li>
          <li>
            <a :href="survey.link_url" target="_blank">{{ survey.link_url }}</a>
          </li>
          <li>
            <div>　|　</div>
          </li>
          <li><button @click="delSurvey(survey.id)">删除</button></li>
        </ul>
        <div class="survey-item__title">
          <p>{{ survey.title }}</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import toggleStopCollect from '@/request/toggleStopCollect'
import deleteSurvey from '@/request/deleteSurvey'
import useAllSurveyData from '@/store/all-survey-data'

export default {
  data() {
    return {
      type: 'allSurvey',
    }
  },
  computed: {
    surveys() {
      return useAllSurveyData()[this.type]
    },
  },
  methods: {
    copyUrl(e) {
      const { id } = e.currentTarget.dataset
      if (navigator.clipboard && window.isSecureContext) {
        // http 属于非安全域, 不支持直接复制
        navigator.clipboard.writeText(window.location.origin + `/fill-in/${id}`).then(() => {
          this.$msg('复制成功, 别人可通过该链接填写问卷', 'success')
        })
      } else {
        const textArea = document.createElement('textarea')
        textArea.value = window.location.origin + `/fill-in/${id}`
        document.body.appendChild(textArea)
        textArea.focus()
        textArea.select()
        if (document.execCommand('copy')) {
          this.$msg('复制成功, 别人可通过该链接填写问卷', 'success')
        } else {
          window.open(window.location.origin + `/fill-in/${id}`, '_blank')
        }
        textArea.remove()
      }
    },
    toStatistics(e) {
      const { id } = e.currentTarget.dataset
      this.$router.push(`/show-statistics/${id}`)
    },
    async toggleOver(id) {
      const status = await toggleStopCollect(id)
      if (!status) {
        this.$msg('修改失败', 'error')
        return
      }
      useAllSurveyData().update()
    },
    async delSurvey(id) {
      if (window.confirm('确定删除该问卷？')) {
        const status = await deleteSurvey(id)
        if (!status) {
          this.$msg('删除失败', 'error')
          return
        }
        useAllSurveyData().update()
      }
    },
  },
  mounted() {
    useAllSurveyData().update()
  },
}
</script>

<style lang="scss">
.top-info {
  height: 50px;
  padding: 10px 30px;
  display: flex;
  align-items: center;
  justify-content: flex-start;

  .numbers {
    flex: none;
    width: max-content;
    margin-right: 20px;
    font-weight: bold;
  }
  .radio-sort {
    flex: none;
    display: flex;
    align-items: center;
    user-select: none;

    div {
      width: 100px;
      height: 34px;
      margin: 0 10px;
      position: relative;

      input {
        width: 100%;
        height: 100%;
        display: block;
        opacity: 0;
        position: absolute;
      }
      label {
        display: block;
        width: 100%;
        height: 100%;
        display: grid;
        place-content: center;
        background-color: #fff;

        font-size: 14px;
        color: #333;
        border-radius: 4px;
        box-shadow: 0 0 4px 0 #ddd;
      }
      input:checked + label {
        background-color: skyblue;
        color: #fff;
      }
    }
  }
}

.survey-area {
  width: 100%;
  height: 100%;
  overflow: auto;
  padding-bottom: 40px;
  flex: 1 0 auto;
  .wrap {
    padding: 18px 30px;
    .survey-item {
      width: 100%;
      min-width: max-content;
      padding: 10px;
      margin: 0 16px 32px 0;
      position: relative;

      background-color: #fff;
      box-shadow: 0px 2px 8px rgb(0 0 0 / 6%);
      border-radius: 4px;

      span.tag {
        position: absolute;
        z-index: 11;
        top: 10px;
        left: -5px;
        --h: 24px;
        width: 70px;
        font-size: 12px;

        color: #fff;
        background-color: #1ed261;
        &.over {
          color: #fff;
          background-color: #d2361e;
        }
      }

      .survey-item__config {
        padding: 0 70px;
        padding-bottom: 10px;
        display: flex;
        align-items: center;

        li {
          width: max-content;
          min-width: max-content;
          margin-right: 20px;
          button {
            border: none;
            font-size: 15px;
            cursor: pointer;
            &:hover {
              color: #0f6bff;
              filter: brightness(1.1);
            }
            &:active {
              filter: brightness(0.9);
            }
          }
          a:hover {
            color: #0f6bff;
          }
        }
        li.li-svg {
          display: flex;
          align-items: center;
          cursor: pointer;
          span {
            cursor: inherit;
            line-height: 20px;
            margin-left: 4px;
          }
          &:hover span {
            color: #0f6bff;
            fill: #0f6bff;
          }
        }
      }
      .survey-item__title {
        width: 100%;
        padding: 0 20px;
        p {
          width: 90%;
          word-wrap: break-word;
        }
      }
    }
  }
}
</style>
